home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Freelog 125
/
Freelog_MarsAvril2015_No125.iso
/
Musique
/
Quod Libet
/
quodlibet-3.3.0-installer.exe
/
bin
/
musicbrainz2
/
utils.pyc
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2014-12-31
|
7KB
|
189 lines
# Source Generated with Decompyle++
# File: in.pyc (Python 2.7)
'''Various utilities to simplify common tasks.
This module contains helper functions to make common tasks easier.
@author: Matthias Friedrich <matt@mafr.de>
'''
__revision__ = '$Id: utils.py 13322 2011-11-03 13:38:06Z luks $'
import re
import urlparse
__all__ = [
'extractUuid',
'extractFragment',
'extractEntityType',
'getReleaseTypeName',
'getCountryName',
'getLanguageName',
'getScriptName']
PATH_PATTERN = '^/(artist|release|track|label|release-group)/([^/]*)$'
def extractUuid(uriStr, resType = None):
"""Extract the UUID part from a MusicBrainz identifier.
\tThis function takes a MusicBrainz ID (an absolute URI) as the input
\tand returns the UUID part of the URI, thus turning it into a relative
\tURI. If C{uriStr} is None or a relative URI, then it is returned
\tunchanged.
\tThe C{resType} parameter can be used for error checking. Set it to
\t'artist', 'release', or 'track' to make sure C{uriStr} is a
\tsyntactically valid MusicBrainz identifier of the given resource
\ttype. If it isn't, a C{ValueError} exception is raised.
\tThis error checking only works if C{uriStr} is an absolute URI, of
\tcourse.
\tExample:
\t>>> from musicbrainz2.utils import extractUuid
\t>>> extractUuid('http://musicbrainz.org/artist/c0b2500e-0cef-4130-869d-732b23ed9df5', 'artist')
\t'c0b2500e-0cef-4130-869d-732b23ed9df5'
\t>>>
\t@param uriStr: a string containing a MusicBrainz ID (an URI), or None
\t@param resType: a string containing a resource type
\t@return: a string containing a relative URI, or None
\t@raise ValueError: the given URI is no valid MusicBrainz ID
\t"""
if uriStr is None:
return None
(scheme, netloc, path) = None.urlparse(uriStr)[:3]
if scheme == '':
return uriStr
if None != 'http' or netloc != 'musicbrainz.org':
raise ValueError('%s is no MB ID.' % uriStr)
m = re.match(PATH_PATTERN, path)
if m:
if resType is None:
return m.group(2)
if None.group(1) == resType:
return m.group(2)
raise None('expected "%s" Id' % resType)
raise ValueError('%s is no valid MB ID.' % uriStr)
def extractFragment(uriStr, uriPrefix = None):
"""Extract the fragment part from a URI.
\tIf C{uriStr} is None or no absolute URI, then it is returned unchanged.
\tThe C{uriPrefix} parameter can be used for error checking. If C{uriStr}
\tis an absolute URI, then the function checks if it starts with
\tC{uriPrefix}. If it doesn't, a C{ValueError} exception is raised.
\t@param uriStr: a string containing an absolute URI
\t@param uriPrefix: a string containing an URI prefix
\t@return: a string containing the fragment, or None
\t@raise ValueError: the given URI doesn't start with C{uriPrefix}
\t"""
if uriStr is None:
return None
(scheme, netloc, path, params, query, frag) = None.urlparse(uriStr)
if scheme == '':
return uriStr
if None is None or uriStr.startswith(uriPrefix):
return frag
raise None("prefix doesn't match URI %s" % uriStr)
def extractEntityType(uriStr):
"""Returns the entity type an entity URI is referring to.
\t@param uriStr: a string containing an absolute entity URI
\t@return: a string containing 'artist', 'release', 'track', or 'label'
\t@raise ValueError: if the given URI is no valid MusicBrainz ID
\t"""
if uriStr is None:
raise ValueError('None is no valid entity URI')
(scheme, netloc, path) = urlparse.urlparse(uriStr)[:3]
if scheme == '':
raise ValueError('%s is no absolute MB ID.' % uriStr)
if scheme != 'http' or netloc != 'musicbrainz.org':
raise ValueError('%s is no MB ID.' % uriStr)
m = re.match(PATH_PATTERN, path)
if m:
return m.group(1)
raise None('%s is no valid MB ID.' % uriStr)
def getReleaseTypeName(releaseType):
'''Returns the name of a release type URI.
\t@param releaseType: a string containing a release type URI
\t@return: a string containing a printable name for the release type
\t@see: L{musicbrainz2.model.Release}
\t'''
releaseTypeNames = releaseTypeNames
import musicbrainz2.data.releasetypenames
return releaseTypeNames.get(releaseType)
def getCountryName(id_):
"""Returns a country's name based on an ISO-3166 country code.
\tThe country table this function is based on has been modified for
\tMusicBrainz purposes by using the extension mechanism defined in
\tISO-3166. All IDs are still valid ISO-3166 country codes, but some
\tIDs have been added to include historic countries and some of the
\tcountry names have been modified to make them better suited for
\tdisplay purposes.
\tIf the country ID is not found, None is returned. This may happen
\tfor example, when new countries are added to the MusicBrainz web
\tservice which aren't known to this library yet.
\t@param id_: a two-letter upper case string containing an ISO-3166 code
\t@return: a string containing the country's name, or None
\t@see: L{musicbrainz2.model}
\t"""
countryNames = countryNames
import musicbrainz2.data.countrynames
return countryNames.get(id_)
def getLanguageName(id_):
"""Returns a language name based on an ISO-639-2/T code.
\tThis function uses a subset of the ISO-639-2/T code table to map
\tlanguage IDs (terminologic, not bibliographic ones!) to names.
\t@param id_: a three-letter upper case string containing an ISO-639-2/T code
\t@return: a string containing the language's name, or None
\t@see: L{musicbrainz2.model}
\t"""
languageNames = languageNames
import musicbrainz2.data.languagenames
return languageNames.get(id_)
def getScriptName(id_):
"""Returns a script name based on an ISO-15924 code.
\tThis function uses a subset of the ISO-15924 code table to map
\tscript IDs to names.
\t@param id_: a four-letter string containing an ISO-15924 script code
\t@return: a string containing the script's name, or None
\t@see: L{musicbrainz2.model}
\t"""
scriptNames = scriptNames
import musicbrainz2.data.scriptnames
return scriptNames.get(id_)